//
//  DatePickerViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/2/18.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit

class DatePickerViewController: QMUICommonViewController,UIPickerViewDelegate,UIPickerViewDataSource,QMUIModalPresentationContentViewControllerProtocol {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
      var selectBlock:((String)->())?
    
    var days:[String] = [String]()
    
    var hours:[String] = [String]()
    
    var mins:[String] = [String]()
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return days.count
        } else if component == 1 {
            return hours.count
        } else if component == 2 {
            return mins.count
        } else {
            return 0
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if component == 0 {
            return 180
        }else {
            return 64
        }
    }
    
    
    func preferredContentSize(in controller: QMUIModalPresentationViewController!, keyboardHeight: CGFloat, limitSize: CGSize) -> CGSize {
        let contentWidth = (UIScreen.main.bounds.width)
        return CGSize(width: contentWidth, height: 250)
    }
    
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return days[row]
        case 1:
            return hours[row]
        case 2:
            return mins[row]
        default:
            return ""
        }
    }
    
    
    
    
    
    @IBOutlet weak var pickerView: UIPickerView!
    
    @IBOutlet weak var btnOk: UIButton!
    @IBOutlet weak var btnCancel: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        setupPickerView()
        // Do any additional setup after loading the view.
    }
    
    func setupPickerView()  {

        self.btnOk.setTitle(LanguageHelper.getString(key: "BtnOK"), for: .normal)
        self.btnCancel.setTitle(LanguageHelper.getString(key: "BtnCancel"), for: .normal)

        let date = Date()
        let fotmatr = DateFormatter()
        fotmatr.dateFormat = "YYYY-MM-dd"
        
        let c = Calendar.current
        let day1 =  c.date(byAdding: .day, value: 1, to: date)
        let day2 =  c.date(byAdding: .day, value: 2, to: date)
        
        days.append(fotmatr.string(from: date))
        days.append(fotmatr.string(from: day1!))
        days.append(fotmatr.string(from: day2!))
        
        for i in 1...23 {
            hours.append("\(i)")
        }
        mins.append("00")
        mins.append("15")
        mins.append("30")
        mins.append("45")
    }
    
    
    @IBAction func btnAction(_ sender: UIButton) {
        if sender.tag == 1 {
//           let row1 = self.pickerView.selectedRow(inComponent: 0)
//            print(row1)
            let day = self.days[self.pickerView.selectedRow(inComponent: 0)]
            let hour = self.hours[self.pickerView.selectedRow(inComponent: 1)]
            let min = self.mins[self.pickerView.selectedRow(inComponent: 2)]
            let time =  "\(day) \(hour):\(min)"
            if selectBlock != nil {
                selectBlock!(time)
            }
            
        }else {
            if selectBlock != nil {
                selectBlock!("")
            }
            
        }
        
    }
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
